<?php

use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class CreateProductStockLogsTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('product_stock_logs', function (Blueprint $table) {
            $table->increments('id');
            $table->integer('product_id')->index()->unsigned()->default(0)->comment('产品ID');
            $table->integer('storage_id')->unsigned()->default(0)->comment('仓库ID');
            $table->tinyInteger('mode')->unsigned()->default(1)->comment('行为模式，1-增加，2-减少');
            $table->tinyInteger('type')->unsigned()->default(1)->comment('类型，1-采购入库，2-销售出库，3-采购退货，4-销售退货');
            $table->integer('original_num')->unsigned()->default(0)->comment('原存量');
            $table->decimal('original_cost_price',8,2)->unsigned()->default(0.00)->comment('原成本价(入库前)');

            $table->integer('move_num')->default(0)->comment('调动量');
            $table->decimal('move_price',8,2)->unsigned()->default(0.00)->comment('调动量的成本单价');
            $table->decimal('move_total_price',8,2)->unsigned()->default(0.00)->comment('调动量的成本总价');

            $table->integer('current_num')->unsigned()->default(0)->comment('当前量');
            $table->decimal('current_cost_price',8,2)->unsigned()->default(0.00)->comment('目前成本价(入库合并后)');
            $table->decimal('current_total_cost_price',8,2)->unsigned()->default(0.00)->comment('目前成本价(入库合并后)');

            $table->index(['product_id', 'storage_id'], 'product_storage_id');

            $table->timestamps();
        });
        \DB::statement("ALTER TABLE `product_stock_logs` comment '库存记录表'");

    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('product_stock_logs');
    }
}
